package com.erp.module.jiehao

import android.content.Context
import android.util.Log
import com.erp.Interface.OnUserDataInterface
import com.erp.bean.JieHaoKBean
import com.erp.pull.BasePullP
import com.erp.util.JsonUtil
import com.erp.util.Myutil
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject

/**
 * Created by Administrator on 2019/3/19/019.
 * 作者：刘洋
 */
class IJieHaoP(var code : Int ,var list :MutableList<JieHaoKBean>,var V : JieHaoContract.JieHaoView,var context:Context) : BasePullP(),JieHaoContract.JieHaoP {


    var md = JieHaoModel()
    var sousuo :String =""
    init {
        PullV=V
    }
    override fun getData(page: Int, pagesize: Int, onUserDataInterface: OnUserDataInterface) {
        md.getJieHao(context,page,pagesize,sousuo,code,onUserDataInterface)
    }

    override fun jsondata(data: String?) {
        try{
            val js =JSONArray(data)
            for(i in 0 until js.length()){
                val jo=js[i] as JSONObject
                var jiehao=JieHaoKBean()
                jiehao.batch_name=JsonUtil.getvolue(jo,"batch_name")
                jiehao.batch_names=JsonUtil.getvolue(jo,"batch_names")
                jiehao.distributed_target=JsonUtil.getvolue(jo,"distributed_target")
                jiehao.is_distributed=JsonUtil.getvolue(jo,"is_distributed")
                jiehao.assign_time=JsonUtil.getvolue(jo,"assign_time")
                jiehao.creator_name=JsonUtil.getvolue(jo,"creator_name")
                jiehao.assignment_name=JsonUtil.getvolue(jo,"assignment_name")
                if(jiehao.batch_names==""){
                    jiehao.batch_names=jiehao.batch_name
                }
                list.add(jiehao)
            }
        }catch (e : JSONException){
            e.printStackTrace()
        }
    }

    override fun deleteJieHao(batch_names: String,postion : Int) {
        md.deletejiehao(context,batch_names,object : OnUserDataInterface{
            override fun onUserData(data: String?) {
                if(data==null){
                    return
                }
                Myutil.MyTost("删除成功")
                V.deleteOk(postion)
            }

            override fun onNot(throwable: String?) {
                if(throwable==null){
                    return
                }
                V.fenPeiJieHaoNo(throwable)
            }

        })
    }


    override fun listClean() {
        list.clear()
    }

    override fun fenPeiJieHao(jieHaoKBean: String, feipei: String, isfenpei: String) {
        V.fenpeijiehao()
        var jo=JSONObject()
        try {
            if(isfenpei=="1") {
                jo.put("distributed_target",feipei)
            }
            jo.put("is_distributed",isfenpei)
        }catch ( e :JSONException){
            e.printStackTrace()
        }
        Log.d("fenPeiJieHao",jo.toString())
        md.setjiehao(context,jo.toString(),jieHaoKBean,code,object : OnUserDataInterface{
            override fun onUserData(data: String) {
                V.fenPeiJieHaoOk(data)
                if(isfenpei=="1"){
                    V.PutTime()
                }
            }

            override fun onNot(throwable: String) {
                V.fenPeiJieHaoNo(throwable)
            }

        })
    }
    override fun sousuo(suosuo: String) {
        this.sousuo=sousuo
        xiala()
    }
}